<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use Qiniu\Auth;
use think\App;
use think\facade\View;
use think\response\Json;

class Course extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Course';

    //关联模型
    protected $hasOne = ['cate', 'user'];

    protected $append = ['type_name', 'is_free_text'];

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addRow(['field' => 'cate', 'title' => '分类', 'templet' => '<div>{{d.cate.title}}</div>'])
            ->addRow(['field' => 'title', 'title' => '标题'])
            ->addRow(['field' => 'is_free_text', 'title' => '是否付费'])
            ->addRow(['field' => 'price', 'title' => '价格'])
            ->addRow(['field' => 'user', 'title' => '分润会员', 'templet' => '<div>{{#  if(d.user){ }}{{d.user.code}}{{#  } }}</div>'])
            ->addRow(['field' => 'type_name', 'title' => '类型'])
            ->addRow(['field' => 'total_hit', 'title' => '总点赞量'])
            ->addRow(['field' => 'total_comment', 'title' => '总评论量'])
            ->addRow(['field' => 'sort', 'title' => '排序', 'width' => 100, 'templet' => '#sort_sort'])
            ->addRow(['field' => 'pick', 'title' => '是否推荐', 'templet' => '#pick', 'width' => 120])
            ->addRow(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addRow(['field' => 'create_time', 'title' => '创建时间', 'width' => 150])
            ->addRow(['field' => 'update_time', 'title' => '更新时间', 'width' => 150])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 150])
            ->clearTable();

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addSwitchTool([['name' => 'pick', 'pk' => 'id', 'text' => '是|否'],
                ['name' => 'status', 'pk' => 'id', 'text' => '显示|隐藏']])
            ->addTopButtons(['add' => ['width' => 1600, 'height' => 1200]])
            ->addRightButtons(['edit' => ['width' => 1600, 'height' => 1200], 'delete'])
            ->addSortTool([['name' => 'sort', 'pk' => 'id', 'url' => url('update')->build()]])
            ->setSearch($this->setSearch(), false)
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['title', '课程标题', 'input', '', '', '', 'like']);
    }

    public function add($name = '', $tpl = 'course/edit')
    {
        $config = config('qiniu');
        // 构建鉴权对象
        $auth = new Auth($config['accessKey'], $config['secretKey']);
        $token = $auth->uploadToken($config['bucket']);
        View::assign('token', $token);
        View::assign('domain', $config['domain']);
        View::assign('cate_list', \app\common\model\CollegeCate::where(['is_del' => 0, 'status' => 1])->select());
        return parent::add($name, $tpl);
    }

    public function edit($name = '', $id = '', $tpl = 'course/edit', $info = '')
    {
        $config = config('qiniu');
        // 构建鉴权对象
        $auth = new Auth($config['accessKey'], $config['secretKey']);
        $token = $auth->uploadToken($config['bucket']);
        View::assign('token', $token);
        View::assign('domain', $config['domain']);
        $info = $this->model::find($id);
        if ($info->type == 'video') {
            $info->video_link = $info->link;
            $info->video_link_url = $config['domain'].$info->link;
            $info->audio_link = '';
            $info->audio_link_url = '';
        } elseif($info->type == 'audio') {
            $info->video_link = '';
            $info->video_link_url = '';
            $info->audio_link = $info->link;
            $info->audio_link_url = $config['domain'].$info->link;
        } else {
            $info->video_link = '';
            $info->video_link_url = '';
            $info->audio_link = '';
            $info->audio_link_url = '';
        }
        View::assign('cate_list', \app\common\model\CollegeCate::where(['is_del' => 0])->select());
        return parent::edit($name, $id, $tpl, $info);
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
